﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Petra.Gis.Geometry;
using Petra.Gis.Geometry.Support;
using Petra.Gis.DataServices.WKT.Support;

namespace Petra.Gis.DataServices.WKT.Support
{
  internal unsafe static class CoordinateListConverter
  {



    internal static string ToString(Geometry.Support.CoordinateList Coords, Dimension dim) 
    {
      string outStr = "";
      string sep = "";

      System.Globalization.CultureInfo cultureEnUS = new System.Globalization.CultureInfo("en-US");

      foreach (ICoordinate c in Coords)
      {
        outStr += sep + c.X.ToString(cultureEnUS) + " " + c.Y.ToString(cultureEnUS);
        sep = ",";
      }

      return outStr;
    }


    
    /*
    //todo: need to manage culture in string !!
    internal static Geometry.Support.CoordinateList ToCoordinateList(Group g, Dimension dim)
    {
      
      int numCoords = g.Length / (int)dim;
      //todo:validate dimension

      return ToCoordinateList(g.Captures, 0, g.Length - 1, dim);
      
    }
     */


    /*internal static Geometry.Support.CoordinateList ToCoordinateList(Group g, int strBeginIndex, int strEndIndex, Dimension dim)
    {
      int i, j;
      //get num of coords
      //get begin coord index
      for (i = 0; i < g.Length; i++)
        if (strBeginIndex < g.Captures[i].Index)
          break;

      //get end coord index
      for (j = i; j < g.Length; j++)
        if (strEndIndex < g.Captures[j].Index) 
        {
          j--;
          break;
        }

      return ToCoordinateList(g.Captures, i, j, dim);

    }

    internal static Geometry.Support.CoordinateList ToCoordinateList(CaptureCollection cc, int start, int end, Dimension dim)
    {

      int numCoords = (end - start + 1) / (int)dim;

      Gis.Geometry.Support.CoordinateList Coords = new CoordinateList(numCoords, dim);
      int j = start;
      for (int i = 0; i < numCoords; i++)
      {

        Coords[i].X = Double.Parse(cc[j].Value);
        Coords[i].Y = Double.Parse(cc[j + 1].Value);
        if (dim == Dimension.dim3D)
          Coords[i].Z = Double.Parse(cc[j + 2].Value);
        //else
        //  Coords[i].Z = 0;

        j += (int)dim;
      }

      return Coords;
    }
    */

    //length = num of coordinates !!
    internal static Geometry.Support.CoordinateList ToCoordinateList(RegexGeometry rg, int startPos, int lenght, Dimension dim)
    {

      int numCoords = lenght;
      int j = startPos;

      Gis.Geometry.Support.CoordinateList Coords = new CoordinateList(numCoords, dim);
      CaptureCollection cc = rg.parser.regexMatch.Groups[rg.groupName].Captures;

      System.Globalization.CultureInfo cultureEnUS = new System.Globalization.CultureInfo("en-US");

      for (int i = 0; i < numCoords; i++)
      {
        Coords[i].X = Double.Parse(cc[j].Value, cultureEnUS);
        Coords[i].Y = Double.Parse(cc[j + 1].Value, cultureEnUS);
        if (dim == Dimension.dim3D)
          Coords[i].Z = Double.Parse(cc[j + 2].Value, cultureEnUS);
        //else
        //  Coords[i].Z = 0;

        j += (int)dim;
      }

      return Coords;

    }


    internal static Geometry.Support.CoordinateList ToCoordinateList(RegexGeometry rg, Dimension dim)
    {

      if (rg.nodeType != RegexGeometry.NodeType.Coordlist)
        throw new ArgumentException("invalid RegexGeometry nodeType", "rg");

      int numCoords = (rg.endIndex - rg.startIndex + 1) / (int)dim;
      int j = rg.startIndex;

      return ToCoordinateList(rg, j, numCoords, dim);

    }

  }
}
